/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gammapeit.jms.ejb;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 *
 * @author Mike
 */
@Stateless
public class MailMessageEJB implements MailMessageEJBLocal {

    @Resource(mappedName = "jms/MailMessagesConn")
    private ConnectionFactory connectionFactory;
    @Resource(mappedName = "jms/MailMessagesQueue")
    private Queue queue;
    
    @Override
    public void enviarMail(String receptores, String mensaje, String asunto) {
        MessageProducer messageProducer;            
        TextMessage textMessage;
        Connection connection;
        Session session;
                
        try {
            connection = connectionFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            messageProducer = session.createProducer(queue);
            textMessage = session.createTextMessage();
            
//            textMessage.setText("Probando, Probando, 1, 2, 3, SSSSSonido.....");
            textMessage.setObjectProperty("receptores", receptores);
            textMessage.setObjectProperty("asunto", asunto);
            textMessage.setObjectProperty("mensaje", mensaje);
//            System.out.println("Enviando el siguiente mensaje: " + textMessage.getText());
            messageProducer.send(textMessage);
            
            messageProducer.close();
            session.close();
            connection.close();
            
        } catch (JMSException ex) {
            Logger.getLogger(MailMessageEJB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

}
